﻿/**
 * Created by arvin on 2017/3/30.
 */

var Bearcat = require('bearcat');
var Async = require("async")

module.exports = function () {
    return Bearcat.getBean({
        id: "get-reflashmotion",
        func: reflashmotion,
        props: [
            {name: "app", "ref": "application"}
        ]
    });
}

function reflashmotion() {
    this.app = null;
}

var CheckMotionDevice = function (msg) {
    if(msg.params.motion == undefined)
        return false;
    //  if(msg.params.motion.length != 40)
    //    return false;
    return true;
}

reflashmotion.prototype.handle = function (msg,next) {
   
    {
        next(null,{code:101,msg:110});
        return;
    }

    var motion = msg.params.motion;
    var motionstate = msg.params.motionstate;
    var model = this.app.getComponent("dao").getConnection().model("MotionDevices");
    Async.waterfall(
        [
            function (cb) {
                model.invalidate(motion,function (invalid) {
                    if(invalid)
                        cb();
                    else
                        next(null, {code: 103, msg: "不存在这个信息"});
                })
            },
            function () {
                model.UpdateMotionState({MotionDevice:motion,MotionState:motionstate},function (result) {
                    if (result)
                        next(null,{code:1,msg:"更新设备成功"});
                });
            }
        ]
    );
}